package com.hsgene.consumables.controller;

import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.consumables.condition.PurchaseConditon;
import com.hsgene.consumables.dto.*;
import com.hsgene.consumables.exception.ConsumablesErrorCode;
import com.hsgene.consumables.service.ConsumablesReceiptService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 耗材入库
 *
 * @author wxf
 * @date 2018/8/15 8:44
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/goods/purchase")
public class ConsumablesReceiptController {

    /**
     * Log
     */
    private static final Logger LOGGER = LogManager.getLogger(ConsumablesReceiptController.class);

    @Autowired
    private ConsumablesReceiptService receiptService;


    /**
     * 查询添加商品的库存、预警等信息
     *
     * @param goodsId 商品id
     * @return
     */
    @GetMapping("query_inventory")
    public ApiResult<List<ConsumablesInventoryInfoDto>> getInventoryInfoByGoodsId(String goodsId) {
        try {
            List<ConsumablesInventoryInfoDto> inventoryInfoDto = receiptService.getInventoryInfoByGoodsId(goodsId);
            return new ApiResult<>(200, "success", "查询成功", inventoryInfoDto);
        } catch (Exception e) {
            LOGGER.error("query inventory info is error", e);
            return ApiResult.fail(ConsumablesErrorCode.QUERY_IS_ERROR);
        }
    }

    /**
     *批量入库
     * @Author FX
     * @date 2018/10/25 14:30
     * @param receiptDto
     * @param userInfo
     * @Return
     */
    @PostMapping
    public ApiResult<Integer> addConsumablesReceiptInfo(@RequestBody ConsumablesReceiptDto receiptDto,@FromSession UserInfo userInfo) {
            receiptDto.setPurchaseMember(userInfo.getRealName());
            int result = receiptService.addConsumablesReceiptInfo(receiptDto.getReceiptInfos(), receiptDto.getPurchaseMember(), receiptDto.getRemarks());
            if(result==-2){
                return new ApiResult(30018, "error", "入库数量不能大于9999", result);
            }
            return new ApiResult(200, "success", "入库成功", result);
    }


    /**
     * 入库记录列表
     * @param
     * @return
     */
    @GetMapping
    public ApiResult<Pagination<GeneticGoodsPurchaseDto>> getReceiptRecordInfo(Integer page,Integer size
            ,@RequestParam(value = "orderField" ,defaultValue = "purchase_date_time") String orderField
            ,String isAsc
            ,PurchaseConditon condition) {
        condition.setLimitHelper(LimitHelper.create(page, size, true));
        String field="b."+orderField;
        boolean isA= Boolean.parseBoolean(isAsc);
        condition.setOrderHelper(OrderHelper.create(field, isA));
        return receiptService.getReceiptRecordInfo(condition);
    }

    /**
     * 获取入库详情
     *
     * @param purchaseNo 入库编号
     * @return
     */
    @GetMapping("{purchaseNo}")
    public ApiResult<List<GeneticGoodsPurchaseDto>> getReceiptDetailInfo(@PathVariable("purchaseNo") String purchaseNo) {
           List<GeneticGoodsPurchaseDto> result = receiptService.getReceiptDetailInfo(purchaseNo);
            return ApiResult.succ(result);
    }
}
